//
//  HWFeedBackViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/1/18.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWFeedBackViewController.h"
#import "HWFeedBackHeadTextView.h"
#import "HWFeedBackCell.h"

@interface HWFeedBackViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)HWFeedBackHeadTextView *headView;

@property (nonatomic, strong)NSString *weixinStr;//微信
@property (nonatomic, strong)NSString *phoneStr;//手机号
@property (nonatomic, strong)NSString *context;//意见
@end

@implementation HWFeedBackViewController

- (HWFeedBackHeadTextView *)headView
{
    if (!_headView)
    {
        _headView = [[HWFeedBackHeadTextView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,264*kUIScaleBasePlus)];
        WeakSelf(self)
        _headView.getTextBlock = ^(NSString *text) {
            weakSelf.context = text;
        };
    }
    return _headView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = LCHexColor(0xF5F5F5);
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self buryPoint];
    [self setTitle:@"意见反馈"];
    [self setDefaultNav];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableView delegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*kUIScaleBasePlus;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10*kUIScaleBasePlus;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 80*kUIScaleBasePlus;
    }
    else
    {
        return 0.01;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1)
    {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,80*kUIScaleBasePlus)];
        footView.backgroundColor = [UIColor clearColor];
        
        UIButton *admitButton = [HWUtilties creatButtonWithFrame:CGRectMake(123*kUIScaleBasePlus,35*kUIScaleBasePlus,130*kUIScaleBasePlus,44*kUIScaleBasePlus)
                                                    textAligment:NSTextAlignmentCenter
                                                            text:@"提交"
                                                        textClor:LCHexColor(0xffffff)
                                                       buttonBGC:LCHexColor(0xF54337)
                                                        textFont:17*kUIScaleBasePlus
                                                          target:self
                                                          method:@selector(admit)];
        admitButton.layer.masksToBounds = YES;
        admitButton.layer.cornerRadius = admitButton.frame.size.height/2;
        
        [footView addSubview:admitButton];
        return footView;
    }
   
    
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWFeedBackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FeedBackCell"];
    if (!cell)
    {
        cell = [[HWFeedBackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FeedBackCell"];
        cell.selectionStyle = 0;
    }
    
    if (indexPath.section == 0)
    {
        cell.iconImageView.image = [UIImage imageNamed:@"write_weixin"];
        cell.textField.delegate = self;
        cell.textField.placeholder = @"请输入您的微信号";
        [cell.textField addTarget:self action:@selector(writeWeixin:) forControlEvents:UIControlEventAllEditingEvents];
    }
    else
    {
        cell.iconImageView.image = [UIImage imageNamed:@"write_telephone"];
        cell.textField.delegate = self;
        cell.textField.placeholder = @"请输入您的手机号码";
        [cell.textField addTarget:self action:@selector(writeTelephone:) forControlEvents:UIControlEventAllEditingEvents];
    }
    
    cell.textField.tag = indexPath.section;
    
    return cell;
}


#pragma mark - textField delegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - 方法
//填写微信
- (void)writeWeixin:(UITextField *)textfield
{
    self.weixinStr = textfield.text;
}
//填写手机号码
- (void)writeTelephone:(UITextField *)textfield
{
    self.phoneStr = textfield.text;
}

//提交
- (void)admit
{
    if (![self.context isValidString])
    {
        [SVProgressHUD showErrorWithStatus:@"意见不能为空"];
        return;
    }

    if (![self.weixinStr isValidString])
    {
        [SVProgressHUD showErrorWithStatus:@"微信不能为空"];
        return;
    }
    
    if (![self.phoneStr isValidString])
    {
        [SVProgressHUD showErrorWithStatus:@"手机号码不能为空"];
        return;
    }
    
    if (![HWUtilties checkTel:self.phoneStr]) return;
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"advice" : self.context,
                               @"wxAccount" : self.weixinStr,
                               @"phoneNum" : self.phoneStr
                               };
    
    WeakSelf(self)
    [MyInfoManager gadmitAdvice:prarmers sucess:^(id  _Nonnull acceptData) {
        if ([acceptData isEqual:@200])
        {
            DISMISS_SVP(1.0f);
            [SVProgressHUD showSuccessWithStatus:@"提交成功"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
       
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"提交意见失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
    
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:@"/feedBack"
                               fromId:nil
                                 toId:@"7b0d3cffab3758d7"];
}

@end
